using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Dynamic;
using YTS.Tools;

namespace Test.ConsoleProgram.Algorithm.Solution
{
    [TestDescription("算法: 1371. 每个元音包含偶数次的最长子字符串")]
    public class No1371_FindTheLongestSubstring : AbsBaseTestItem
    {
        public override void OnTest()
        {
            Assert.TestExe(FindTheLongestSubstring, "eleetminicoworoep", 13);
            Assert.TestExe(FindTheLongestSubstring, "leetcodeisgreat", 5);
            Assert.TestExe(FindTheLongestSubstring, "bcbcbc", 6);
        }

        public int FindTheLongestSubstring(string s)
        {
            int[] earliest = new int[1 << 5];
            for (int i = 0; i < earliest.Length; i++)
            {
                earliest[i] = int.MaxValue;
            }
            earliest[0] = -1;
            int mask = 0, ans = 0;
            for (int i = 0; i < s.Length; i++)
            {
                switch (s[i])
                {
                    case 'a': mask ^= (1 << 0); break;
                    case 'e': mask ^= (1 << 1); break;
                    case 'i': mask ^= (1 << 2); break;
                    case 'o': mask ^= (1 << 3); break;
                    case 'u': mask ^= (1 << 4); break;
                }
                if (earliest[mask] == int.MaxValue)
                {
                    earliest[mask] = i;
                }
                else
                {
                    ans = Math.Max(ans, i - earliest[mask]);
                }
            }
            return ans;
        }
    }
}
